﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Phoenix.AppCode.Entities;
using System.Data.SqlClient;
using System.Data;

namespace Phoenix.AppCode.Data
{
    public class TrackingEDAO : BaseDAO
    {

        public DataTable GetTrackByNumber(TrackingE tracking)
        {
            using (SqlConnection connection = GetConnection())
            {
                connection.Open();
                SqlCommand command = new SqlCommand("sp_TrackingEListByNumberTrackingE", connection);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@NumberTrackingE", tracking.NumberTrackingE));
                command.Parameters.Add(new SqlParameter("@Language", tracking.Language));
                SqlDataAdapter sda = new SqlDataAdapter(command);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                command.ExecuteNonQuery();
                return dt;
            }
        }

        #region Backend method
        /// <summary>
        /// Insert Article Category
        /// </summary>
        /// <param name="trackinge"></param>
        public void Create(TrackingE trackinge)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spTrackingECreate", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@NumberTrackingE", trackinge.NumberTrackingE));
                command.Parameters.Add(new SqlParameter("@PostUser", trackinge.PostUser));

                connection.Open();
                command.ExecuteNonQuery();
                //if (paramId.Value != DBNull.Value)
                //TrackingE.ID = (long)paramId.Value;
                //else
                //  throw new DataAccessException(String.Format(Resources.Strings.CreateEntityException, "Category"));
            }
        }

        /// <summary>
        /// Edit Article Category
        /// </summary>
        /// <param name="category"></param>
        public void Edit(TrackingE trackinge)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spTrackingEEdit", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@ID_TrackingE", trackinge.ID_TrackingE));
                command.Parameters.Add(new SqlParameter("@NumberTrackingE", trackinge.NumberTrackingE));
                command.Parameters.Add(new SqlParameter("@EditUser", trackinge.EditUser));

                connection.Open();
                command.ExecuteNonQuery();
                //if (command.ExecuteNonQuery() <= 0)
                //    throw new DataAccessException(String.Format(Resources.Strings.UpdateEntityException, "Category", category.ID));
            }
        }

        /// <summary>
        /// On/Off view
        /// </summary>
        /// <param name="trackinge"></param>
        public void OnOff(TrackingE trackinge)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spTrackingEOnOff", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@ID_TrackingE", trackinge.ID_TrackingE));

                connection.Open();
                command.ExecuteNonQuery();
                //if (command.ExecuteNonQuery() <= 0)
                //    throw new DataAccessException(String.Format(Resources.Strings.UpdateEntityException, "Category", category.ID));
            }
        }


        /// <summary>
        /// Delete TrackingE
        /// </summary>
        /// <param name="category"></param>
        public void Delete(TrackingE trackinge)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spTrackingEDelete", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@ID_TrackingE", trackinge.ID_TrackingE));

                connection.Open();
                command.ExecuteNonQuery();
                //if (command.ExecuteNonQuery() <= 0)
                //    throw new DataAccessException(String.Format(Resources.Strings.DeleteEntityException, "Category", category.ID));
            }
        }

        /// <summary>
        /// Show TrackingE All
        /// </summary>
        /// <returns></returns>
        public DataTable TrackingEList()
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spTrackingEList", connection);
                command.CommandType = CommandType.StoredProcedure;

                SqlDataAdapter da = new SqlDataAdapter(command);

                connection.Open();
                DataTable dt = new DataTable();
                da.Fill(dt);

                return dt;
            }
        }

        /// <summary>
        /// Show data by ID
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        public DataTable TrackingEList_GetByID(int idtrackinge)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spTrackingEEdit_GetByID", connection);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add("@ID_TrackingE", SqlDbType.Int).Value = idtrackinge;
                SqlDataAdapter da = new SqlDataAdapter(command);

                connection.Open();
                DataTable dt = new DataTable();
                da.Fill(dt);

                return dt;
            }
        }

        /// <summary>
        /// Get ID to Edit or Delete
        /// </summary>
        /// <param name="idtrackinge"></param>
        /// <returns></returns>
        public TrackingE GetTrackingEByID(int idtrackinge)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spTrackingEEdit_GetByID", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@ID_TrackingE", idtrackinge));

                connection.Open();
                TrackingE TrackingE = null;
                using (IDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    if (reader.Read())
                        TrackingE = CreateTrackingEFromReader(reader);
                    //else
                    //   throw new DataAccessException(String.Format(Resources.Strings.GetEntityException, "Category", ID));
                }

                return TrackingE;
            }
        }

        private TrackingE CreateTrackingEFromReader(IDataReader reader)
        {
            TrackingE trackinge = new TrackingE();
            trackinge.ID_TrackingE = (int)reader["ID_TrackingE"];
            trackinge.NumberTrackingE = (string)reader["NumberTrackingE"];
            trackinge.PostDate = (DateTime)reader["PostDate"];
            trackinge.PostUser = (string)reader["PostUser"];
            trackinge.EditDate = (DateTime)reader["EditDate"];
            trackinge.EditUser = (string)reader["EditUser"];
            trackinge.Visible = (Boolean)reader["Visible"];
            return trackinge;
        }
        #endregion
    }
}